echo "======================================================================="
echo "=                                                                     ="
echo "=                                                                     ="
echo "=                            Make x265                                ="
echo "=                                                                     ="
echo "=                                                                     ="
echo "======================================================================="

cd ../

basepath=$(cd `dirname $0`; pwd)
echo ${basepath}

cd ${basepath}/

if [ -d ./x265_git-build ];then
    rm -r x265_git-build
fi

cp -r x265_git x265_git-build

chmod -R 777 x265_git-build

cd ${basepath}/x265_git-build/

if [ -d ./x265_install ];then
    rm -r x265_install
fi

if [ -d ./x265_build_8bit ];then 
    rm -rf x265_build_8bit
fi
mkdir x265_build_8bit

if [ -d ./x265_build_10bit ];then 
    rm -rf x265_build_10bit
fi
mkdir x265_build_10bit

if [ -d ./x265_build_12bit ];then 
    rm -rf x265_build_12bit
fi
mkdir x265_build_12bit


start=$(date +%s)


cd ${basepath}/x265_git-build/x265_build_12bit
cmake ../source -DENABLE_LIBNUMA=OFF -DHIGH_BIT_DEPTH=ON -DEXPORT_C_API=OFF -DENABLE_SHARED=OFF -DENABLE_CLI=OFF -DMAIN12=ON
make clean
make -j16

cd ${basepath}/x265_git-build/x265_build_10bit
cmake ../source -DENABLE_LIBNUMA=OFF -DHIGH_BIT_DEPTH=ON -DEXPORT_C_API=OFF -DENABLE_SHARED=OFF -DENABLE_CLI=OFF
make clean
make -j16

cd ${basepath}/x265_git-build/x265_build_8bit

ln -sf ../x265_build_10bit/libx265.a libx265_main10.a
ln -sf ../x265_build_12bit/libx265.a libx265_main12.a

cmake ../source \
-DCMAKE_INSTALL_PREFIX=${basepath}/x265_git-build/x265_install \
-DENABLE_SHARED=OFF \
-DENABLE_LIBNUMA=OFF \
-DEXTRA_LIB="x265_main10.a;x265_main12.a" \
-DEXTRA_LINK_FLAGS=-L. \
-DLINKED_10BIT=ON -DLINKED_12BIT=ON \
-DENABLE_CLI=ON

make clean
make -j16

mv libx265.a libx265_main.a

ar -M <<EOF
CREATE libx265.a
ADDLIB libx265_main.a
ADDLIB libx265_main10.a
ADDLIB libx265_main12.a
SAVE
END
EOF

make install


end=$(date +%s)

duration=$(( end - start ))
echo "x265 cost: $duration s"